<?php

namespace App\Http\Controllers\sendcode;

use App\Http\Controllers\Controller;
use App\Server\Sms;
use Illuminate\Http\Request;

class SendCodeController extends Controller
{
    /**
     * use 李耀
     * 发送验证码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * http://www.test.com/sendCode
     */
    public function sendCode(Request $request){
        try {
            $phone=$request->post('phone');
            $rand= Sms::SendMessage($phone);
            return response()->json(['code'=>200,'msg'=>'Ok','data'=>[$rand]]);
        } catch(\Exception $exception){
            return response()->json(['code'=>200,'msg'=>$exception->getMessage(),
                'data'=>[]]);
        }
    }

    public function register(Request $request){
        $request->validate([
            'phone' => 'required|regex:/^1\d{10}$/',
        ],[
            'phone.required'=>'手机号不能为空',
            'phone.regex'=>'手机号不能小于11位并且必须为数字',##
        ]);
        try {
            $param=$request->input();
            Sms::register($param);
            return response()->json(['code'=>2001,'msg'=>'已注册请登录','data'=>[]]);
        } catch (\Exception $exception){
            return response()->json(['code'=>2001,'msg'=>$exception->getMessage(),'data'=>[]]);
        }
    }
}
